import 'package:edu/constants/app_strings.dart';
import 'package:edu/pages/course/course_detail/course_detail_page_logic.dart';
import 'package:edu/pages/course/course_detail/review/course_detail_review.dart';
import 'package:edu/pages/course/course_detail/tab/course_brief/course_brief.dart';
import 'package:edu/pages/course/course_detail/tab/course_catalog/course_catalog.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class CourseDetailTab extends StatefulWidget {
  const CourseDetailTab({super.key});

  @override
  State<CourseDetailTab> createState() => _CourseDetailTabState();
}

class _CourseDetailTabState extends State<CourseDetailTab> {
  late List<Widget> _pages;

  @override
  void initState() {
    super.initState();

    _pages = [_buildBrief(), _buildCatalog(), _buildReview()];
  }

  // 构建简介(课程简介、讲师介绍、能学到的内容)
  Widget _buildBrief() {
    return Selector<CourseDetailPageLogic, bool>(
      selector: (context, logic) => logic.isContentEmpty,
      builder: (context, v, child) {
        if (v) {
          return const Center(child: Text('暂无课程相关介绍'));
        } else {
          final logic = context.read<CourseDetailPageLogic>();
          return CourseBrief(courseDetail: logic.courseDetail!);
        }
      },
    );
  }

  // 构建课程目录
  Widget _buildCatalog() {
    return const CourseCatalog();
  }

  // 构建课程评价
  Widget _buildReview() {
    return const CourseDetailReview();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _pages.length,
      initialIndex: context.read<CourseDetailPageLogic>().currentTabIndex,
      child: Column(children: [_buildTabBar(context), _buildTabBody(context)]),
    );
  }

  Widget _buildTabBar(BuildContext context) {
    final bgColor = Theme.of(context).colorScheme.surface;
    return Container(
      height: 44,
      width: double.infinity,
      color: bgColor,
      child: _tabBar(context),
    );
  }

  TabBar _tabBar(BuildContext context) {
    return TabBar(
      dividerHeight: 0.5,
      dividerColor: Theme.of(context).colorScheme.surface,
      isScrollable: true,
      tabAlignment: TabAlignment.center,
      labelPadding: const EdgeInsets.symmetric(horizontal: 20),
      labelStyle: const TextStyle(fontWeight: FontWeight.bold, fontSize: 17),
      unselectedLabelStyle: const TextStyle(fontSize: 17),
      // 标签
      tabs: const [
        // 课程简介
        Tab(text: AppStrings.courseBrief),
        // 课程目录
        Tab(text: AppStrings.courseCatalog),
        // 课程评论
        Tab(text: AppStrings.courseComment),
      ],
    );
  }

  Widget _buildTabBody(BuildContext context) {
    return Selector<CourseDetailPageLogic, int>(
      selector: (context, logic) => logic.currentTabIndex,
      builder: (context, v, child) {
        final logic = context.read<CourseDetailPageLogic>();
        final controller = DefaultTabController.of(context);
        controller.addListener(() {
          if (controller.index != logic.currentTabIndex) {
            logic.changeTabIndex(controller.index);
          }
        });
        return Expanded(
          child: SafeArea(child: TabBarView(children: _pages)),
        );
      },
    );
  }
}
